package com.sum.tea.xinlian

import android.content.Intent
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import com.sum.common.base.BaseMvvmActivity
import com.sum.tea.contants.AppConfig
import com.sum.tea.databinding.ActivitySystemlayoutBinding
import com.sum.tea.xinlian.viewmodel.SubSystemViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
 * 系统配置
 *
 * @author Administrator
 */
class SubSystemActivity : BaseMvvmActivity<ActivitySystemlayoutBinding, SubSystemViewModel>() {
    override fun initView(savedInstanceState: Bundle?) {
        val app =  AppConfig.getInstance()
        mViewModel.initialize(app)

        mBinding.apply {
            val threadModeAdapter = ArrayAdapter(
                this@SubSystemActivity,
                android.R.layout.simple_spinner_item,
                mViewModel.strThMode
            )
            threadModeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
            spinnerThread.adapter = threadModeAdapter

            checkBoxSound.isChecked = app.issound

            buttonSaveparam.setOnClickListener {
                lifecycleScope.launch(Dispatchers.IO) {
                    try {
                        app.spf.SaveReaderParams(app.Rparams)
                        runOnUiThread {
                            Toast.makeText(
                                this@SubSystemActivity,
                                AppConfig.Constr_subsysexefin,
                                Toast.LENGTH_SHORT
                            ).show()
                        }
                    } catch (ex: Exception) {
                        runOnUiThread {
                            Toast.makeText(
                                this@SubSystemActivity,
                                AppConfig.Constr_subsysavefailed,
                                Toast.LENGTH_SHORT
                            ).show()
                        }
                    }
                }
            }

            buttonParamdefault.setOnClickListener {
                lifecycleScope.launch(Dispatchers.IO) {
                    mViewModel.setDefaultParams()
                    runOnUiThread {
                        Toast.makeText(
                            this@SubSystemActivity,
                            AppConfig.Constr_subsysreavaid,
                            Toast.LENGTH_SHORT
                        ).show()
                    }
                }
            }

            buttonThreadset.setOnClickListener {
                mViewModel.setThreadMode(spinnerThread.selectedItemPosition)
            }

            buttonLocate.setOnClickListener {
                startActivityForResult(Intent(this@SubSystemActivity, SubPathActivity::class.java), 0)
            }

            buttonOutfile.setOnClickListener {
                val filePath = editTextPath.text.toString()
                if (!filePath.contains("txt") && !filePath.contains("csv")) {
                    Toast.makeText(this@SubSystemActivity, AppConfig.Constr_subsysout, Toast.LENGTH_SHORT).show()
                } else {
                    lifecycleScope.launch(Dispatchers.IO) {
                        mViewModel.exportTagData(filePath)?.let {
                            runOnUiThread {
                                Toast.makeText(this@SubSystemActivity, it, Toast.LENGTH_SHORT).show()
                            }
                        }
                    }
                }
            }

            buttonSoundset.setOnClickListener {
                app.issound = checkBoxSound.isChecked
            }
        }
    }


}
